package com.itogis.rest.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.itogis.rest.po.SysRightInfo;

@Component
public class RoleDao extends BaseDao {
	/**
	 * 添加角色用户
	 * 
	 * @param userid：用户ID
	 * @return roleid：角色ID
	 * @throws SQLException
	 */
	public boolean addUser(String userid,String roleid) {
		String sql = " insert into sys_roleuser_r(roleid,userid) values(" + roleid+","+userid+")";
		Connection con = this.getConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = con.createStatement();
			int b = st.executeUpdate(sql);
			return (b==1)?true:false;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} finally {
			this.closeConnection(rs, st, con);
		}
	}

	/**
	 * 删除用户
	 * 
	 * @param userid：用户ID
	 * @param roleid：角色ID
	 * @return
	 */
	public boolean deleteUser(String userid,String roleid) {
		String sql = " delete sys_roleuser_r where  roleid=" + roleid+",userid="+userid;
		Connection con = this.getConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = con.createStatement();
			int b = st.executeUpdate(sql);
			return (b==1)?true:false;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} finally {
			this.closeConnection(rs, st, con);
		}
	}

	/**
	 * 
	 * 添加角色权限
	 * @param rightid：权限ID
	 * @param roleid:角色ID
	 * @return
	 */
	public boolean addRight(String rightid,String roleid) {
		String sql = " insert into sys_roleright_r(roleid,rightid) values(" + roleid+","+rightid+")";
		Connection con = this.getConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = con.createStatement();
			int b = st.executeUpdate(sql);
			return (b==1)?true:false;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} finally {
			this.closeConnection(rs, st, con);
		}
	}

	/**
	 * 删除角色权限
	 * 
	 * @param rightid：权限ID
	 * @param roleid：角色ID
	 * @return
	 */
	public boolean deleteRight(String rightid,String roleid) {
		String sql = " delete sys_roleright_r where  roleid=" + roleid+",rightid="+rightid;
		Connection con = this.getConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = con.createStatement();
			boolean b = st.execute(sql);
			return b;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} finally {
			this.closeConnection(rs, st, con);
		}
	}
}
